package com.jztd.servone.invoice.param;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.ObjectUtil;
import com.jztd.platfrom.base.exception.BusinessException;
import com.jztd.platfrom.base.exception.InvalidArgumentException;
import lombok.Data;

import java.math.BigDecimal;
import java.util.Date;

@Data
public class ApplyInvoiceDetailsParam {
    /**
     * 是否合开
     */
    private Boolean merge;
    /**
     * 客户端编码
     */
    private String clientCode;
    /**
     * 发票抬头
     */
    private String buyerName;
    /**
     * 统一社会信用代码/纳税人识别号
     */
    private String buyerTaxpayerNum;
    /**
     * 收票人邮箱
     */
    private String takerEmail;
    /**
     * 项目名称
     */
    private String projectName;
    /**
     * 开票金额
     */
    private BigDecimal totalAmount;
    /**
     * 出行人
     */
    private String traveler;
    /**
     * 出行日期
     */
    private String travelDate;
    /**
     * 出行人证件号码
     */
    private String travelerIdNo;
    /**
     * 出发地
     */
    private String departurePlace;
    /**
     * 到达地
     */
    private String destinationPlace;
    /**
     * 备注
     */
    private String remark;
    /**
     * 交通工具类型
     */
    private String vehicleType;
    /**
     * 发票码
     */
    private String invoiceCodes;
    /**
     * 租户id
     */
    private String tenantId;

    /**
     * 数据校验
     */
    public void checkData() {
        if (ObjectUtil.isEmpty(clientCode)) {
            throw new InvalidArgumentException("权限不足");
        }
        if (ObjectUtil.isEmpty(buyerName)) {
            throw new InvalidArgumentException("发票抬头不能为空");
        }
        if (ObjectUtil.isEmpty(totalAmount) || totalAmount.compareTo(BigDecimal.ZERO) <= 0) {
            throw new InvalidArgumentException("开票金额必须大于0");
        }
        if (ObjectUtil.isEmpty(takerEmail)) {
            throw new InvalidArgumentException("接收邮箱不能为空");
        }
        if (ObjectUtil.isEmpty(travelDate)) {
            throw new InvalidArgumentException("出行日期不能为空");
        }
        Date travel = DateUtil.parse(travelDate, "yyyy-MM-dd");
        Date today = DateUtil.beginOfDay(new Date());
        if (!travel.before(today)) {
            throw new BusinessException("请在出发后次日在开票");
        }
        if(ObjectUtil.isNotEmpty(travelerIdNo) && !IdcardUtil.isValidCard(travelerIdNo)){
            throw new BusinessException("身份证格式有误，请检查后重试");
        }
    }
}
